package com.cdl.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cdl.entity.User;
import com.cdl.response.DataResponse;
import com.cdl.response.FormResponse;
import com.cdl.response.PageResponse;
import com.cdl.response.SimpleResponse;
import com.cdl.service.UserService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("user")
public class UserController {

	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	UserService userServiceImpl ;

	/**
	 * 跳转到主页面
	 * @return
	 */
	@RequestMapping("/main")
	public String  toMain(){
		logger.info("跳转到主页面-->user/main");
		return "user/main";
	}
	
	
	/**
	 * 跳转到查询页面
	 * @return
	 */
	@RequestMapping("/query")
	public String  toQuery(){
		logger.info("跳转到查询页面-->user/query");
		return "user/query";
	}
	
	/**
	 * 跳转到列表页面
	 * @return
	 */
	@RequestMapping("/list")
	public String  toList(){
		logger.info("跳转到列表页面-->user/list");
		return "user/list";
	}
	
	
	/**
	 * 跳转到操作表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/form")
	public String toForm(){
		logger.info("跳转到操作表单页面-->user/form");
		return "user/form";
	}
	
	/**
	 * 跳转到明细页面
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	public String toDetail(String id){
		logger.info("跳转到明细页面-->user/detail");
		return "user/detail";
	}
	
	
	/***
	 * 查询用户列表
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getList")
	public PageResponse<User> getList(User user){
		logger.info("获取数据列表--->"+user.toString());
		PageInfo<User> page = userServiceImpl.getPage(user);
		PageResponse<User> response = new PageResponse<User>() ;
		response.setData(page);
		return response ;
	}
	
	/**
	 * 查询用户信息单条
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get")
	public DataResponse<User> get(User user){
		logger.info("获取单条数据--->"+user.toString());
		User data = userServiceImpl.get(user);
		DataResponse<User> response = new DataResponse<User>();
		response.setData(data);
		return response ;
	}
	
	/***
	 * 新增用户信息
	 * @param user
	 */
	@ResponseBody
	@RequestMapping("/add")
	public FormResponse<String> add(User user){
		logger.info("新增数据-->"+user.toString());
		FormResponse<String> response = new FormResponse<String>();
		if(response.isSuccess()){
			userServiceImpl.add(user);
			response.setData(user.getUserId());
			response.setMessage("新增成功！");
		}else{
			response.setMessage("新增失败！");
		}
		return response;
	}
	
	/***
	 * 修改用户信息
	 * @param user
	 */
	@ResponseBody
	@RequestMapping("/update")
	public FormResponse<Void> update(User user){
		logger.info("修改数据-->"+user.toString());
		FormResponse<Void> response = new FormResponse<Void>();
		if(response.isSuccess()){
			userServiceImpl.update(user);
			response.setMessage("修改成功！");
		}else{
			response.setMessage("修改失败！");
		}
		return response;
	}
	
	/**
	 * 删除用户信息
	 * @param user
	 */
	@ResponseBody
	@RequestMapping("/del")
	public SimpleResponse<Void> delete(User user){
		logger.info("删除数据-->"+user.toString());
		SimpleResponse<Void> response = new SimpleResponse<Void>();
		userServiceImpl.delete(user);
		response.setSuccess(true);
		return response;
	}
}
